# advanced_transitions.py
from moviepy.editor import *
import numpy as np

class TransitionEffects:
    def fade_transition(self, clip1, clip2, duration=1.0):
        """淡入淡出转场"""
        # 首先 clip1 淡出，同时 clip2 淡入
        faded_out = clip1.crossfadeout(duration)
        faded_in = clip2.crossfadein(duration)
        return CompositeVideoClip([faded_out, faded_in.set_start(clip1.duration - duration)])

    def slide_transition(self, clip1, clip2, direction="left", duration=1.0):
        """滑动转场"""
        # 获取视频尺寸
        w, h = clip1.size
        
        # 根据方向设置移动距离
        if direction == "left":
            offset = lambda t: (int(-w * min(1.0, t / duration)), 0)
        elif direction == "right":
            offset = lambda t: (int(w * min(1.0, t / duration)), 0)
        elif direction == "up":
            offset = lambda t: (0, int(-h * min(1.0, t / duration)))
        elif direction == "down":
            offset = lambda t: (0, int(h * min(1.0, t / duration)))
        else:
            offset = lambda t: (0, 0)  # 默认无移动
        
        # 移动第二个视频片段
        moving_clip2 = clip2.set_position(offset)
        
        # 组合视频
        final_duration = clip1.duration + duration
        return CompositeVideoClip([
            clip1,
            moving_clip2.set_start(clip1.duration - duration)
        ], size=clip1.size).set_duration(final_duration)

    def zoom_transition(self, clip1, clip2, duration=1.0):
        """缩放转场"""
        # clip1 逐渐缩小，clip2 逐渐放大
        def zoom_out(t):
            # 计算缩放比例，从1.0到0.5
            progress = min(1.0, t / clip1.duration)
            scale_factor = 1 - 0.5 * progress
            return max(0.5, scale_factor)  # 最小缩放到0.5倍

        def zoom_in(t):
            # 计算缩放比例，从0.5到1.0
            progress = min(1.0, (t - (clip1.duration - duration)) / duration) if t >= clip1.duration - duration else 0
            scale_factor = 0.5 + 0.5 * max(0, progress)
            return min(1.0, scale_factor)

        zoomed_out_clip1 = clip1.resize(zoom_out)
        zoomed_in_clip2 = clip2.resize(zoom_in).set_start(clip1.duration - duration)
        
        return CompositeVideoClip([
            zoomed_out_clip1,
            zoomed_in_clip2
        ], size=clip1.size)

    def rotate_transition(self, clip1, clip2, duration=1.0):
        """旋转转场"""
        def rotate(t):
            # 旋转角度，从0到360度
            progress = min(1.0, (t - (clip1.duration - duration)) / duration) if t >= clip1.duration - duration else 0
            angle = 360 * max(0, progress)
            return angle

        rotated_clip2 = clip2.set_start(clip1.duration - duration)
        # 注意：moviepy中旋转需要特殊处理
        return CompositeVideoClip([
            clip1,
            rotated_clip2
        ], size=clip1.size)

    def blur_transition(self, clip1, clip2, duration=1.0):
        """模糊转场"""
        from moviepy.video.fx.all import blur
        
        # 为简化，这里使用淡入淡出方式结合模糊效果
        def blur_filter1(t):
            # clip1 模糊程度随时间增加
            progress = min(1.0, t / clip1.duration)
            blur_level = 2 * progress  # 最大模糊级别为2
            return blur_level

        def blur_filter2(t):
            # clip2 模糊程度随时间减少
            progress = min(1.0, (t - (clip1.duration - duration)) / duration) if t >= clip1.duration - duration else 0
            blur_level = 2 * (1 - max(0, progress))  # 从模糊到清晰
            return max(0, blur_level)

        # 为简化，这里直接使用淡入淡出效果
        blurred_clip1 = clip1
        blurred_clip2 = clip2
        faded_out = clip1.crossfadeout(duration)
        faded_in = clip2.crossfadein(duration).set_start(clip1.duration - duration)
        
        return CompositeVideoClip([faded_out, faded_in], size=clip1.size)
